package com.tencent.crazypenguin.dao;

import javax.servlet.http.HttpServletRequest;

import com.tencent.crazypenguin.bean.Comment;
import org.apache.commons.collections.map.HashedMap;
import org.apache.struts2.ServletActionContext;
import org.hibernate.Session;
import org.hibernate.query.Query;

import com.tencent.crazypenguin.bean.User;
import com.tencent.crazypenguin.config.HbmUtil;
import com.tencent.crazypenguin.config.ResultType;
import java.text.SimpleDateFormat;

import java.util.HashMap;
import java.util.*;

public class UserDao extends BaseDao<User> {

	CommentDao commentDao = new CommentDao();
	
	@SuppressWarnings("rawtypes")
	public int authority(String loginName,String passWord) {
		
		Session s = HbmUtil.getSession();
		
		try{
			
			s.beginTransaction();
			
			Query query = s.createQuery("from User where user=:user");
			
			query.setParameter("user",loginName);
			
			User user = (User) query.getSingleResult();
			
			s.getTransaction().commit();
			
			if(user == null){
				return ResultType.USERNAME_NOT_EXIST;
			}
			
			if(user.getPwd().equals(passWord)){
				HttpServletRequest request = ServletActionContext.getRequest();
				request.setAttribute("user", user);
				return ResultType.SUCEESS;
			}
			else{
				return ResultType.INVALID_PSW;
			}
	
		}catch(Exception e){
			
			e.printStackTrace();
			return ResultType.EXCEPTION;
			
		}finally{
			if(s!=null){
				s.close();
			}

		}
	}


	public User userinfo (String name)throws Exception 
	{
		Session s = HbmUtil.getSession();
		try{
		    
			s.beginTransaction();
			Query query = s.createQuery("from User where username = '"+name+"'");			
			User user = (User) query.getSingleResult();
			s.getTransaction().commit();
			return user;
			}catch(Exception e){
			
			e.printStackTrace();
			
		}finally{
			if(s!=null){
				s.close();
			}

		}
		return null;
		
	}



	/**
	 * 选择用户——YES
	 * @param PARTNER
	 * @param loginUser
	 * @return
	 */
	public int chooseUserY(String PARTNER,String loginUser) {
		Session s = null;
		try{
			s =  HbmUtil.getSession();
			s.beginTransaction();
			//更新当前用户表partner字段
			String updUserPerData = "UPDATE USER_INF SET PARTNER =: PER, P_TIME = NOW(), P_COM = 0 WHERE USER =: LOGINUSER";
			Query queryUPD = s.createQuery(updUserPerData);
			queryUPD.setParameter("PER",PARTNER);
			queryUPD.setParameter("LOGINUSER",loginUser);
			queryUPD.executeUpdate();
			//插入成行时间及默认评分
//			String insCommentData = "INSERT INTO COMMENT_INF(ID_DOCOM, ID_GETCOM, TIME_GO, STAR_COM, STAR_COM)  SELECT USER, PARTNER, P_TIME, P_COM FROM USER_INF WHERE USER = '"+loginUser+"'";
//			Query queryINS = s.createQuery(updUserPerData).executeUpdate();
			//提交事务
			s.getTransaction().commit();
			return ResultType.SUCEESS;
		}catch(Exception e){
			e.printStackTrace();
			s.getTransaction().rollback();
			return ResultType.EXCEPTION;
		}finally{
			if(s!=null){
				s.close();
			}

		}

	}
	/**
	 * 选择用户——NO
	 * @param loginUser
	 * @return
	 */
	public int chooseUserN(String loginUser) {
		Session s = null;
		try{
			s =  HbmUtil.getSession();
			s.beginTransaction();
			//更新当前用户表partner字段
			String updUserPerData = "UPDATE USER_INF SET IS_ACTIVE = 0 WHERE USER =: LOGINUSER;";
			Query queryUPD = s.createQuery(updUserPerData);
			queryUPD.setParameter("LOGINUSER",loginUser);
			queryUPD.executeUpdate();
			//提交事务
			s.getTransaction().commit();
			return ResultType.SUCEESS;
		}catch(Exception e){
			e.printStackTrace();
			s.getTransaction().rollback();
			return ResultType.EXCEPTION;
		}finally{
			if(s!=null){
				s.close();
			}

		}

	}

	/***
	 * 根据评分用户id返回给这个用户好评的用户列表和时间
	 * @param id
     */
	public List<Comment> queryEvaluateeList(long id){
		List<Comment> list = commentDao.queryEvaluateeList(id,"star_com","desc");
		return list;
	}
	

}
